package com.hanagent.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.hanagent.model.Agents;
import com.hanagent.model.Moneyrecords;
import com.hanagent.utils.ConstantUtils;
import com.hanagent.utils.DateUtils;
import com.hanagent.utils.MoneyUtils;
import com.hanagent.utils.ResponseUtils;
import com.hanagent.utils.StrUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class MoneyController extends Controller{
	
	public void getList(){
		Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
		String agentId = getPara("agentId");
		
		String startCreateTime = getPara("startCreateTime");
		if (StrUtils.isEmpty(startCreateTime)) {
			startCreateTime = "1917-12-05 16:00:00";
		} 
		String endCreateTime = getPara("endCreateTime");
		if (StrUtils.isEmpty(endCreateTime)) {
			endCreateTime = "3017-12-05 16:00:00";
		}  
		String startUpdateTime = getPara("startUpdateTime");
		if (StrUtils.isEmpty(startUpdateTime)) {
			startUpdateTime = "1917-12-05 16:00:00";
		} 
		String endUpdateTime = getPara("endUpdateTime");
		if (StrUtils.isEmpty(endUpdateTime)) {
			endUpdateTime = "3017-12-05 16:00:00";
		} 
		
		int pageNumber = getParaToInt("pageNumber")<1?1:getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize")<1?1:getParaToInt("pageSize");
		String type = getPara("type");
		
		startCreateTime = DateUtils.getDateStrWithPassHour(startCreateTime, 8);
		endCreateTime = DateUtils.getDateStrWithPassHour(endCreateTime, 32);
		startUpdateTime = DateUtils.getDateStrWithPassHour(startUpdateTime, 8);
		endUpdateTime = DateUtils.getDateStrWithPassHour(endUpdateTime, 32);
		
		double allMoney = 0;
		double allMoneyCompanyTurnover = 0;
		double allMoneySaleAgentFir = 0;
		double allMoneySaleAgentSec = 0;
		double allMoneyAiXin = 0;
		double allMoneyLvYou = 0;
		double allMoneyPostalLevel = 0;
		
		Page<Moneyrecords> list = null;
		if (StrUtils.isEmpty(agentId)) {
			if (loginAgents.getAgentLevel()>ConstantUtils.LevelCompanySource){
				if (StrUtils.isEmpty(type)) {
					list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select MoneyRecords.*, Agents.agentMan , AgentParent.agentMan as parentAgentMan", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  INNER JOIN Agents AgentParent "
							+ "ON AgentParent.agentId = Agents.agentParentId  where MoneyRecords.userOrAgentId = ? and (MoneyRecords.type < "+ ConstantUtils.MonneyTypePostalLevel
							+ " or MoneyRecords.type = " + ConstantUtils.MonneyTypeBalanceExChange + " or MoneyRecords.type = " + ConstantUtils.MonneyTypeBalancePay 
							+ ") and MoneyRecords.createdAt > ? "
							+ "and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", loginAgents.getAgentId(), startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
				}else {
					list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select MoneyRecords.*, Agents.agentMan , AgentParent.agentMan as parentAgentMan", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  INNER JOIN Agents AgentParent "
							+ "ON AgentParent.agentId = Agents.agentParentId  where MoneyRecords.userOrAgentId = ? and MoneyRecords.type = "+ type 
							+ " and MoneyRecords.createdAt > ? and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", loginAgents.getAgentId(), startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
				}
				allMoneyCompanyTurnover = getDatePrice(loginAgents.getAgentId()+"", ConstantUtils.MonneyTypeCompanyTurnover+"", startCreateTime, endCreateTime);
				allMoneySaleAgentFir = getDatePrice(loginAgents.getAgentId()+"", ConstantUtils.MonneyTypeSaleFirstAgent10+"", startCreateTime, endCreateTime);
				allMoneySaleAgentSec = getDatePrice(loginAgents.getAgentId()+"", ConstantUtils.MonneyTypeSaleSecAgent5+"", startCreateTime, endCreateTime);
				allMoneyAiXin = getDatePrice(loginAgents.getAgentId()+"", ConstantUtils.MonneyTypeAixin+"", startCreateTime, endCreateTime);
				allMoneyLvYou = getDatePrice(loginAgents.getAgentId()+"", ConstantUtils.MonneyTypeLvYou+"", startCreateTime, endCreateTime);
				allMoneyPostalLevel = getDatePrice(loginAgents.getAgentId()+"", ConstantUtils.MonneyTypePostalLevel+"", startCreateTime, endCreateTime);
			}else{
				if (StrUtils.isEmpty(type)) {
					list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select MoneyRecords.*, Agents.agentMan , AgentParent.agentMan as parentAgentMan", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  INNER JOIN Agents AgentParent "
							+ "ON AgentParent.agentId = Agents.agentParentId where (MoneyRecords.type < "+ ConstantUtils.MonneyTypePostalLevel
							+ " or MoneyRecords.type = " + ConstantUtils.MonneyTypeBalanceExChange + " or MoneyRecords.type = " + ConstantUtils.MonneyTypeBalancePay 
							+ ") and MoneyRecords.createdAt > ? "
							+ "and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
				}else{
					list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select MoneyRecords.*, Agents.agentMan , AgentParent.agentMan as parentAgentMan", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  INNER JOIN Agents AgentParent "
							+ "ON AgentParent.agentId = Agents.agentParentId  where MoneyRecords.type = "+ type +"  and MoneyRecords.createdAt > ? "
							+ "and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
				}
				allMoneyCompanyTurnover = getDatePrice("", ConstantUtils.MonneyTypeCompanyTurnover+"", startCreateTime, endCreateTime);
				//总部的分红是原始的分红  不是扣除旅游基金   爱心基金之后的分红
//				allMoneyCompanyTurnover = MoneyUtils.formatDouble(allMoneyCompanyTurnover/0.97);
				allMoneySaleAgentFir = getDatePrice("", ConstantUtils.MonneyTypeSaleFirstAgent10+"", startCreateTime, endCreateTime);
				allMoneySaleAgentSec = getDatePrice("", ConstantUtils.MonneyTypeSaleSecAgent5+"", startCreateTime, endCreateTime);
				allMoneyAiXin = getDatePrice("", ConstantUtils.MonneyTypeAixin+"", startCreateTime, endCreateTime);
				allMoneyLvYou = getDatePrice("", ConstantUtils.MonneyTypeLvYou+"", startCreateTime, endCreateTime);
				allMoneyPostalLevel = getDatePrice("", ConstantUtils.MonneyTypePostalLevel+"", startCreateTime, endCreateTime);
			}
		}else{
			if (loginAgents.getAgentLevel()!=ConstantUtils.LevelSuper && !agentId.equals(loginAgents.getAgentId()+"")) {
				renderText(ResponseUtils.sendResult(0, "", "你无权查看其他代理商的收益情况"));
				return;
			}
			if (StrUtils.isEmpty(type)) {
				list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select MoneyRecords.*, Agents.agentMan ", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  where MoneyRecords.userOrAgentId = ?  and MoneyRecords.createdAt > ? "
							+ "and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", agentId, startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
			}else{
				list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select MoneyRecords.*, Agents.agentMan ", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  where MoneyRecords.userOrAgentId = ? and MoneyRecords.type = "+ type 
						+"  and MoneyRecords.createdAt > ? and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
						+ "order by MoneyRecords.updatedAt desc", agentId, startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);;
			}
			allMoneyCompanyTurnover = getDatePrice(agentId, ConstantUtils.MonneyTypeCompanyTurnover+"", startCreateTime, endCreateTime);
			allMoneySaleAgentFir = getDatePrice(agentId, ConstantUtils.MonneyTypeSaleFirstAgent10+"", startCreateTime, endCreateTime);
			allMoneySaleAgentSec = getDatePrice(agentId, ConstantUtils.MonneyTypeSaleSecAgent5+"", startCreateTime, endCreateTime);
			allMoneyAiXin = getDatePrice(agentId, ConstantUtils.MonneyTypeAixin+"", startCreateTime, endCreateTime);
			allMoneyLvYou = getDatePrice(agentId, ConstantUtils.MonneyTypeLvYou+"", startCreateTime, endCreateTime);
			allMoneyPostalLevel = getDatePrice(agentId, ConstantUtils.MonneyTypePostalLevel+"", startCreateTime, endCreateTime);
		}
		
		JSONObject object = new JSONObject();
		allMoney = MoneyUtils.formatDouble(allMoneyCompanyTurnover+allMoneySaleAgentFir+allMoneySaleAgentSec+allMoneyAiXin+allMoneyLvYou);
		object.put("allMoney", allMoney);
		object.put("allMoneyCompanyTurnover", allMoneyCompanyTurnover);
		object.put("allMoneySaleAgentFir", allMoneySaleAgentFir);
		object.put("allMoneySaleAgentSec", allMoneySaleAgentSec);
		object.put("allMoneyAiXin", allMoneyAiXin);
		object.put("allMoneyLvYou", allMoneyLvYou);
		object.put("allMoneyPostalLevel", allMoneyPostalLevel);
		object.put("listData", list); 
		
        renderText(ResponseUtils.sendResult(1, object, "获取数据成功")); 
	}
	
	private double getDatePrice(String agentId, String type, String startCreateTime, String endCreateTime){
		Record datePriceSum = null;
		
		if (StrUtils.isEmpty(agentId)) {
			if (StrUtils.isEmpty(type)) {
				datePriceSum = Db.findFirst("select sum(moneyCount) as datePrice from MoneyRecords where type<" + ConstantUtils.MonneyTypePostalLevel
						+" and MoneyRecords.createdAt > '" + startCreateTime + "' and MoneyRecords.createdAt < '" + endCreateTime +"'");
			}else{
				datePriceSum = Db.findFirst("select sum(moneyCount) as datePrice from MoneyRecords where " 
						+"type = "+ type +" and MoneyRecords.createdAt > '" + startCreateTime + "' and MoneyRecords.createdAt < '" + endCreateTime +"'");
			}
		}else{
			if (StrUtils.isEmpty(type)) {
				datePriceSum = Db.findFirst("select sum(moneyCount) as datePrice from MoneyRecords where userOrAgentId="+ agentId
						+  " and type<" + ConstantUtils.MonneyTypePostalLevel
						+" and MoneyRecords.createdAt > '" + startCreateTime + "' and MoneyRecords.createdAt < '" + endCreateTime +"'");
			}else{
				datePriceSum = Db.findFirst("select sum(moneyCount) as datePrice from MoneyRecords where userOrAgentId="+ agentId 
						+" and type = "+ type +" and MoneyRecords.createdAt > '" + startCreateTime + "' and MoneyRecords.createdAt < '" + endCreateTime +"'");
			}
		}
		 
		double datePrice = 0; 
		if (datePriceSum.get("datePrice")!=null) {
			datePrice =  datePriceSum.getDouble("datePrice");
		}
		datePrice = MoneyUtils.formatDouble(datePrice);
		return datePrice;
	}
	
	public void getMoneyJijin() {
		Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
		String agentId = getPara("agentId");
		
		String startCreateTime = getPara("startCreateTime");
		if (StrUtils.isEmpty(startCreateTime)) {
			startCreateTime = "1917-12-05 16:00:00";
		} 
		String endCreateTime = getPara("endCreateTime");
		if (StrUtils.isEmpty(endCreateTime)) {
			endCreateTime = "3017-12-05 16:00:00";
		}  
		String startUpdateTime = getPara("startUpdateTime");
		if (StrUtils.isEmpty(startUpdateTime)) {
			startUpdateTime = "1917-12-05 16:00:00";
		} 
		String endUpdateTime = getPara("endUpdateTime");
		if (StrUtils.isEmpty(endUpdateTime)) {
			endUpdateTime = "3017-12-05 16:00:00";
		} 
		
		int pageNumber = getParaToInt("pageNumber")<1?1:getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize")<1?1:getParaToInt("pageSize");
		String type = getPara("type");
		
		startCreateTime = DateUtils.getDateStrWithPassHour(startCreateTime, 8);
		endCreateTime = DateUtils.getDateStrWithPassHour(endCreateTime, 32);
		startUpdateTime = DateUtils.getDateStrWithPassHour(startUpdateTime, 8);
		endUpdateTime = DateUtils.getDateStrWithPassHour(endUpdateTime, 32);
		
		double allMoneyAiXin = 0;
		double allMoneyLvYou = 0;
		
		Page<Moneyrecords> list = null;
		if (StrUtils.isEmpty(agentId)) {
			if (loginAgents.getAgentLevel()>ConstantUtils.LevelCompanySource){
				if (StrUtils.isEmpty(type)) {
					list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select MoneyRecords.*, Agents.agentMan ", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId where (MoneyRecords.type = " + ConstantUtils.MonneyTypeAixin + 
							" or MoneyRecords.type = "+ ConstantUtils.MonneyTypeLvYou +") and MoneyRecords.userOrAgentId = ? and MoneyRecords.createdAt > ? "
							+ "and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", loginAgents.getAgentId(), startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
				}else {
					list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select *, Agents.agentMan ", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  where MoneyRecords.userOrAgentId = ? and MoneyRecords.type = "+ type 
							+ " and MoneyRecords.createdAt > ? and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", loginAgents.getAgentId(), startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
				}
				allMoneyAiXin = getDatePrice(loginAgents.getAgentId()+"", ConstantUtils.MonneyTypeAixin+"", startCreateTime, endCreateTime);
				allMoneyLvYou = getDatePrice(loginAgents.getAgentId()+"", ConstantUtils.MonneyTypeLvYou+"", startCreateTime, endCreateTime);
			}else{
				if (StrUtils.isEmpty(type)) {
					list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select *, Agents.agentMan ", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId where (MoneyRecords.type = " + ConstantUtils.MonneyTypeAixin + 
							" or MoneyRecords.type = "+ ConstantUtils.MonneyTypeLvYou +") and MoneyRecords.createdAt > ? "
							+ "and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
				}else{
					list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select *, Agents.agentMan ", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  where MoneyRecords.type = "+ type +"  and MoneyRecords.createdAt > ? "
							+ "and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
				}
				allMoneyAiXin = getDatePrice("", ConstantUtils.MonneyTypeAixin+"", startCreateTime, endCreateTime);
				allMoneyLvYou = getDatePrice("", ConstantUtils.MonneyTypeLvYou+"", startCreateTime, endCreateTime);
			}
		}else{
			if (loginAgents.getAgentLevel()!=ConstantUtils.LevelSuper && !agentId.equals(loginAgents.getAgentId()+"")) {
				renderText(ResponseUtils.sendResult(0, "", "你无权查看其他代理商的收益情况"));
				return;
			}
			if (StrUtils.isEmpty(type)) {
				list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select *, Agents.agentMan ", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  where (MoneyRecords.type = " + ConstantUtils.MonneyTypeAixin + 
							" or MoneyRecords.type = "+ ConstantUtils.MonneyTypeLvYou +") and MoneyRecords.userOrAgentId = ?  and MoneyRecords.createdAt > ? "
							+ "and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
							+ "order by MoneyRecords.updatedAt desc", agentId, startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
			}else{
				list = Moneyrecords.dao.paginate(pageNumber, pageSize, "select *, Agents.agentMan ", "from MoneyRecords INNER JOIN Agents "
							+ "ON Agents.agentId = MoneyRecords.userOrAgentId  where MoneyRecords.userOrAgentId = ? and MoneyRecords.type = "+ type 
						+"  and MoneyRecords.createdAt > ? and MoneyRecords.createdAt < ? and MoneyRecords.updatedAt > ? and MoneyRecords.updatedAt < ? "
						+ "order by MoneyRecords.updatedAt desc", agentId, startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);;
			}
			allMoneyAiXin = getDatePrice(agentId, ConstantUtils.MonneyTypeAixin+"", startCreateTime, endCreateTime);
			allMoneyLvYou = getDatePrice(agentId, ConstantUtils.MonneyTypeLvYou+"", startCreateTime, endCreateTime);
		}
		
		JSONObject object = new JSONObject();
		object.put("allMoneyAiXin", allMoneyAiXin);
		object.put("allMoneyLvYou", allMoneyLvYou);
		object.put("allJiJin", MoneyUtils.formatDouble(allMoneyLvYou+allMoneyAiXin));
		object.put("listData", list); 
		
        renderText(ResponseUtils.sendResult(1, object, "获取数据成功")); 
	}
	
	public void getMoneyInfo() {
		Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
		int agentId = loginAgents.getAgentId();
		String agentIdStr = getPara("agentId");
		if (!StrUtils.isEmpty(agentIdStr)) {
			agentId = Integer.parseInt(agentIdStr);
		}
		double moneyCount = MoneyUtils.getAllMoneyCount(agentId);
		double allPostalMoney = MoneyUtils.getAllPostalMoney(agentId);
		double allWaitPostalMoney = MoneyUtils.getWaitPostalMoney(agentId);
		double canPostal = moneyCount - allPostalMoney - allWaitPostalMoney;
		JSONObject object = new JSONObject();
		object.put("moneyCount", MoneyUtils.formatDouble(moneyCount));
		object.put("allPostalMoney", MoneyUtils.formatDouble(allPostalMoney));
		object.put("allWaitPostalMoney", MoneyUtils.formatDouble(allWaitPostalMoney));
		object.put("canPostal", MoneyUtils.formatDouble(canPostal));
		renderText(ResponseUtils.sendResult(1, object, "获取数据成功"));
	}
	
	
}
